﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;

namespace Bank_database_system
{
    public partial class 流水查询a : UserControl
    {
        int NowPage = 1;
        int PageNumber = 1;
        decimal PureProfit = 0;
        public 流水查询a()
        {
            InitializeComponent();
            DateTime today = DateTime.Now;
            DateTime firstDayOfMonth = new DateTime(today.Year, today.Month, 1);
            dateTimePicker1.Value = firstDayOfMonth;
            DateTime lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);
            dateTimePicker2.Value = lastDayOfMonth;

            DataTable dataTable = Finance.Select_Transaction(AccountCheck.branchid, dateTimePicker1.Value.ToShortDateString(), dateTimePicker2.Value.ToShortDateString(), NowPage, 财务首页.PageSize);
            dataGridView1.DataSource = dataTable;
            dataGridView1.RowHeadersVisible = false;
            dataGridView1.AllowUserToAddRows = false; // 禁用添加空白行
            dataGridView1.AllowUserToResizeColumns = false;
            dataGridView1.AllowUserToResizeRows = false;
            dataGridView1.ReadOnly = true;
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            foreach (DataGridViewColumn column in dataGridView1.Columns)
            {
                column.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                column.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
            }
            PageNumber = Finance.GetTransactionPageNumber(AccountCheck.branchid, dateTimePicker1.Value.ToShortDateString(), dateTimePicker2.Value.ToShortDateString());
            label7.Text = PageNumber.ToString();
            label6.Text = NowPage.ToString();
            label5.Text = Finance.GetExpenses(AccountCheck.branchid, dateTimePicker1.Value.ToShortDateString(), dateTimePicker2.Value.ToShortDateString()).ToString() + " CNY";
            label1.Text = Finance.GetIncome(AccountCheck.branchid, dateTimePicker1.Value.ToShortDateString(), dateTimePicker2.Value.ToShortDateString()).ToString() + " CNY";
            PureProfit = Finance.GetIncome(AccountCheck.branchid, dateTimePicker1.Value.ToShortDateString(), dateTimePicker2.Value.ToShortDateString()) - Finance.GetExpenses(AccountCheck.branchid, dateTimePicker1.Value.ToShortDateString(), dateTimePicker2.Value.ToShortDateString());
            if (PureProfit > 0)
            {
                label15.Text = "+" + PureProfit.ToString() + " CNY";
            }
            else if (PureProfit <= 0)
            {
                label15.Text = PureProfit.ToString() + " CNY";
            }
            if (PageNumber == 0)
            {
                NowPage = 0;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            NowPage = 1;
            DateTime today = DateTime.Now;
            DateTime firstDayOfMonth = new DateTime(today.Year, today.Month, 1);
            dateTimePicker1.Value = firstDayOfMonth;
            DateTime lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1);
            dateTimePicker2.Value = lastDayOfMonth;

            DataTable dataTable = Finance.Select_Transaction(AccountCheck.branchid, dateTimePicker1.Value.ToShortDateString(), dateTimePicker2.Value.ToShortDateString(), NowPage, 财务首页.PageSize);
            dataGridView1.DataSource = dataTable;
            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            PageNumber = Finance.GetTransactionPageNumber(AccountCheck.branchid, dateTimePicker1.Value.ToShortDateString(), dateTimePicker2.Value.ToShortDateString());
            label7.Text = PageNumber.ToString();
            label6.Text = NowPage.ToString();
            label5.Text = Finance.GetExpenses(AccountCheck.branchid, dateTimePicker1.Value.ToShortDateString(), dateTimePicker2.Value.ToShortDateString()).ToString() + " CNY";
            label1.Text = Finance.GetIncome(AccountCheck.branchid, dateTimePicker1.Value.ToShortDateString(), dateTimePicker2.Value.ToShortDateString()).ToString() + " CNY";
            PureProfit = Finance.GetIncome(AccountCheck.branchid, dateTimePicker1.Value.ToShortDateString(), dateTimePicker2.Value.ToShortDateString()) - Finance.GetExpenses(AccountCheck.branchid, dateTimePicker1.Value.ToShortDateString(), dateTimePicker2.Value.ToShortDateString());
            if (PureProfit > 0)
            {
                label15.Text = "+" + PureProfit.ToString() + " CNY";
            }
            else if (PureProfit <= 0)
            {
                label15.Text = PureProfit.ToString() + " CNY";
            }
            if (PageNumber == 0)
            {
                NowPage = 0;
            }
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox5_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void textBox7_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox7_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox6_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            NowPage = 1;
            DataTable dataTable = Finance.Select_Transaction(AccountCheck.branchid, dateTimePicker1.Value.ToShortDateString(), dateTimePicker2.Value.ToShortDateString(), NowPage, 财务首页.PageSize);
            if (dataTable.Rows.Count > 0)
            {
                dataGridView1.DataSource = dataTable;
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                PageNumber = Finance.GetTransactionPageNumber(AccountCheck.branchid, dateTimePicker1.Value.ToShortDateString(), dateTimePicker2.Value.ToShortDateString());
                label7.Text = PageNumber.ToString();
                label6.Text = NowPage.ToString();
                label5.Text = Finance.GetExpenses(AccountCheck.branchid, dateTimePicker1.Value.ToShortDateString(), dateTimePicker2.Value.ToShortDateString()).ToString() + " CNY";
                label1.Text = Finance.GetIncome(AccountCheck.branchid, dateTimePicker1.Value.ToShortDateString(), dateTimePicker2.Value.ToShortDateString()).ToString() + " CNY";
                PureProfit = Finance.GetIncome(AccountCheck.branchid, dateTimePicker1.Value.ToShortDateString(), dateTimePicker2.Value.ToShortDateString()) - Finance.GetExpenses(AccountCheck.branchid, dateTimePicker1.Value.ToShortDateString(), dateTimePicker2.Value.ToShortDateString());
                if (PureProfit > 0)
                {
                    label15.Text = "+" + PureProfit.ToString() + " CNY";
                }
                else if (PureProfit <= 0)
                {
                    label15.Text = PureProfit.ToString() + " CNY";
                }
                if (PageNumber == 0)
                {
                    NowPage = 0;
                }
            }
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {

        }

        private void 流水查询a_Load(object sender, EventArgs e)
        {

        }

        private void button12_Click(object sender, EventArgs e)
        {
            PageNumber = Finance.GetTransactionPageNumber(AccountCheck.branchid, dateTimePicker1.Value.ToShortDateString(), dateTimePicker2.Value.ToShortDateString());
            label7.Text = PageNumber.ToString();
            NowPage -= 1;
            if (NowPage <= 0)
            {
                NowPage = 1;
                if (PageNumber == 0)
                {
                    NowPage = 0;
                }
            }
            DataTable dataTable = Finance.Select_Transaction(AccountCheck.branchid, dateTimePicker1.Value.ToShortDateString(), dateTimePicker2.Value.ToShortDateString(), NowPage, 财务首页.PageSize);
            if (dataTable.Rows.Count > 0 || PageNumber == 0)
            {
                dataGridView1.DataSource = dataTable;
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                label6.Text = NowPage.ToString();
            }
        }

        private void button13_Click(object sender, EventArgs e)
        {
            PageNumber = Finance.GetTransactionPageNumber(AccountCheck.branchid, dateTimePicker1.Value.ToShortDateString(), dateTimePicker2.Value.ToShortDateString());
            label7.Text = PageNumber.ToString();
            NowPage += 1;
            if (NowPage > PageNumber)
            {
                NowPage = PageNumber;
            }
            DataTable dataTable = Finance.Select_Transaction(AccountCheck.branchid, dateTimePicker1.Value.ToShortDateString(), dateTimePicker2.Value.ToShortDateString(), NowPage, 财务首页.PageSize);
            if (dataTable.Rows.Count > 0 || PageNumber == 0)
            {
                dataGridView1.DataSource = dataTable;
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                label6.Text = NowPage.ToString();
            }
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
